package cn.hn.monkey.stock;

import static cn.hn.monkey.stock.tools.WhatHandler.MSG_LOAD_DAYS_DATA_BEGIN;
import static cn.hn.monkey.stock.tools.WhatHandler.MSG_LOAD_DAYS_DATA_CALCUTE;
import static cn.hn.monkey.stock.tools.WhatHandler.MSG_LOAD_DAYS_DATA_END;
import static cn.hn.monkey.stock.tools.WhatHandler.MSG_LOAD_DAYS_DATA_FINISHED;
import static cn.hn.monkey.stock.tools.WhatHandler.MSG_LOAD_DAYS_DATA_NOT;
import static cn.hn.monkey.stock.tools.WhatHandler.MSG_LOAD_DAYS_DATA_PROCESS;

import android.app.Application;
import android.content.Context;
import android.os.Environment;
import android.os.Handler;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.hn.monkey.stock.data.DaysData;
import cn.hn.monkey.stock.data.TodayData;
import cn.hn.monkey.stock.tools.ZipTools;

public class BaseApplication extends Application {

    static DaysData daysData;
    static TodayData todayData;

    @Override
    public void onCreate() {
        super.onCreate();
        daysData = DaysData.getInstance();
        todayData = TodayData.getInstance();
    }

    public void destroy() {
        todayData.destroy();
    }

    static class UnzipDayData implements Runnable {

        Context context;
        Handler handler;

        UnzipDayData(Context context, Handler handler) {
            this.context = context;
            this.handler = handler;
        }

        public void destroy() {
            handler = null;
        }

        @Override
        public void run() {
            File shzip = new File(Environment.getExternalStorageDirectory(), "Clockwise/shday.zip");
            File szzip = new File(Environment.getExternalStorageDirectory(), "Clockwise/szday.zip");
            ZipTools.ZipToolsListener listener = new ZipTools.ZipToolsListener() {
                @Override
                public void onUnzipFile(final String filename, final byte[] buffer, final int size) {
                    daysData.acceptDayDataForZipFile(filename, buffer, size, handler, MSG_LOAD_DAYS_DATA_PROCESS);
                }

                @Override
                public void onUnzipStatus(boolean begin, boolean end) {
                    if(begin) daysData.UnzipBegin(handler, MSG_LOAD_DAYS_DATA_BEGIN);
                    if(end) {
                        daysData.UnzipEnd(handler, MSG_LOAD_DAYS_DATA_END);
                        daysData.parseZhenfu(handler, MSG_LOAD_DAYS_DATA_CALCUTE);
                        if(handler != null) handler.sendEmptyMessage(MSG_LOAD_DAYS_DATA_FINISHED);
                    }
                }
            };
            if(handler != null) {
                listener.onUnzipStatus(true,false);
                if(shzip.exists() && szzip.exists()) {
                    ZipTools.unzip(shzip.getAbsolutePath(), listener);
                    ZipTools.unzip(szzip.getAbsolutePath(), listener);
                    listener.onUnzipStatus(false,true);
                } else {
                    handler.sendEmptyMessage(MSG_LOAD_DAYS_DATA_NOT);
                }
            }
            System.gc();
        }
    }

    public void refreshTodayData(Handler handler) {
        todayData.startFresh(handler);
    }
    public void stopRefreshTodayData() { todayData.stop(); }

    public List<DaysData.DayDataStock> getStockList(int count) {
        return daysData.getStockList(count);
    }
}
